//{include file=".layers.js"}//
(function () {
    document.write('<script type="text/javascript" src="base.js"></script>');
    var core = ecui,
        ui = core.ui,
        util = core.util,
        dom = core.dom,
        isToucher = document.ontouchstart !== undefined,
        isPointer = !!window.PointerEvent, // 使用pointer事件序列，请一定在需要滚动的元素上加上touch-action:none
        iosVersion = /(iPhone|iPad).*?OS (\d+(_\d+)?)/i.test(navigator.userAgent) ? +(RegExp.$2.replace('_', '.')) : undefined,
        QQVersion = /QQLiveBrowser\/(\d+\.\d)/i.test(navigator.userAgent) ? +RegExp.$1 : undefined;
    var socket;
    //{if 0}//
    setTimeout(function () {
    //{/if}//
        // mao.util.initSocket();
        if (mao.info.isWeixin) {
            mao.util.wxSignature();
        }
        if (mao.info.isDingTalk) {
            mao.util.ddSignature();
        }
    //{if 0}//
    }, 300);
    //{/if}//

    ecui.esr.onready = function () {
        ecui.dom.getParent = ecui.dom.parent;
        ecui.util.extend = Object.assign;
        ecui.ext.esr = ecui.ext.data;
        ecui.triggerEvent = ecui.dispatchEvent;

        mao.cities = ecui.ui.BCities.prototype.CITIES;
        ecui.ui.Select.prototype.TEXTNAME = 'code';
        ecui.esr.DEFAULT_PAGE = '/home/index';
        ecui.render = {};
        ecui.render.select = function (data) {
            this.removeAll(true);
            this.add(data);
        };
        window.onresize = function () {
            if (mao.info.wxReady && ['INPUT','TEXTAREA'].indexOf(document.activeElement.tagName) < 0) {
                ecui.esr.getRoute('/home/index').refreshPage = true;
            }
        };
        dom.addEventListener(
            window,
            'resize',
            function() {
                if (mao.info.wxReady && history.length === 2) {
                    location.reload();
                }
            }, 
            {
                passive: false
            }
        );
        
        return {
            model: [
                //{if 0}//'weixin@GET /erp-management/weixin/openid?code=' + CODE, //{/if}//
                'baseInfo@ /erp-management/base/info',
                'financeBaseInfo@ /erp-management/basic/info',
                'venues@GET /erp-management/venue/select-list-all',
                'staffTree@GET /erp-management/staff/select-list-tree',
                'banks@GET /erp-management/basic/banks',
                'bankCities@GET /erp-management/basic/cities',
                'venuearea@GET /erp-management/venue/select-area-venue',
                'permission@GET /erp-management/permission/perm-code',
                'staffInfo@GET /erp-management/staff/personal/info'
            ],
            children: {
                model: [
                    'unreadCount@GET /erp-management/message/unread-count'
                ],
                onbeforerender: function (context) {
                    ecui.delegate('navTab', null, function () {
                        var navTab = ecui.get('navTab');
                        var count = dom.last(dom.first(navTab.getItem(0).getMain()));

                        if (context.unreadCount.count) {
                            count.innerHTML = context.unreadCount.count > 99 ? '99+' : context.unreadCount.count;
                            dom.removeClass(count, 'ui-hide');
                        } else {
                            count.innerHTML = '';
                            dom.addClass(count, 'ui-hide');
                        }
                    });
                }
            },
            onbeforerequest: function () {

            },
            onbeforerender: function (context) {

                mao.info.baseInfo = mao.info.baseInfo || {};
                Object.assign(mao.info.baseInfo, context.baseInfo, context.financeBaseInfo);

                mao.info.baseInfoMap = mao.info.baseInfoMap || {};
                for (var key in mao.info.baseInfo) {
                    if (mao.info.baseInfo.hasOwnProperty(key)) {
                        mao.info.baseInfoMap[key] = {};
                        mao.info.baseInfo[key].forEach(function(item) {
                            if (item && item instanceof Object) {
                                mao.info.baseInfoMap[key][item.id || item.code] = item.text || item.name;
                            }
                        });
                    }
                }

                mao.info.baseInfo.permission = context.permission;

                mao.info.staffTree = context.staffTree;

                // 遍历出部门树
                function mapDeptTree(tree) {
                    var dept = tree.filter(function (item) { return item.type === 1; }).map(function (item) { return Object.assign({}, item); });
                    dept.forEach(function (item) {
                        if (item.children.length) {
                            item.children = mapDeptTree(item.children);
                        }
                    });
                    return dept;
                }
                mao.info.deptTree = mapDeptTree(context.staffTree);

                // 统计部门子节点数量
                mao.util.mapTree(mao.info.deptTree[0], util.blank, function (tree) {
                    tree.dCount = tree.children.length ? tree.children.reduce(function (a, b) {
                        return a + b.dCount + 1;
                    }, 0) : 0;
                });

                // 统计部门子节点人员数量
                mao.util.mapTree(mao.info.staffTree[0], util.blank, function (tree) {
                    var dCount = 0, sCount = 0;
                    tree.children.forEach(function (item) {
                        if (item.type === 2) {
                            sCount++;
                        } else {
                            sCount += item.sCount;
                            dCount += item.dCount + 1;
                        }
                    });
                    tree.dCount = dCount;
                    tree.sCount = sCount;
                });

                mao.info.banks = context.banks;
                mao.info.bankCities = context.bankCities;
                mao.venues = context.venues;
                mao.venuesMap = [];
                context.venues && context.venues.forEach(function (item) { mao.venuesMap[item.id] = item.name; });
                mao.staffInfo = context.staffInfo;
                mao.staffInfo.storeManager = false;
                mao.staffInfo.venueName = mao.staffInfo.venues.map(function (item) { return mao.venuesMap[item]; }).join('，');
                for (var i = 0, item; item = mao.staffInfo.roles[i++]; ) {
                    if (item === '1000') {
                        mao.staffInfo.storeManager = true;
                        break;
                    }
                }
            }
        };
    };
    document.write('<script type="text/javascript" src="_include/index.controls.js"></script>');
    document.write('<script type="text/javascript" src="_include/ext.js"></script>');
    document.write('<script type="text/javascript" src="_include/common.js"></script>');
    document.write('<script type="text/javascript" src="_include/index.data.js"></script>');
    document.write('<script type="text/javascript" src="_include/filter.js"></script>');
}());
